package mgrclient

import (
	"context"
	apiv1 "gomod.alauda.cn/mysql-mgr-operator/api/v1"
	apiv1a1 "gomod.alauda.cn/mysql-mgr-operator/api/v1alpha1"
	rdsv1 "gomod.alauda.cn/rds-operator/api/v1"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/types"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

func GetMgrCr(ctx context.Context, namespace string, name string) (*apiv1a1.MySQLCluster, error) {
	nn := types.NamespacedName{
		Namespace: namespace,
		Name:      name,
	}
	cr := new(apiv1a1.MySQLCluster)
	err := Client().Get(ctx, nn, cr)
	return cr, err
}

func GetMysqlCr(ctx context.Context, namespace string, name string) (*rdsv1.Mysql, error) {
	nn := types.NamespacedName{
		Namespace: namespace,
		Name:      name,
	}
	cr := new(rdsv1.Mysql)
	err := Client().Get(ctx, nn, cr)
	if err != nil {
		return nil, err
	}
	return cr, nil
}

func GetMetaCr(ctx context.Context, namespace string, name string) (*apiv1.MySQLMeta, error) {
	nn := types.NamespacedName{
		Namespace: namespace,
		Name:      name,
	}
	cr := new(apiv1.MySQLMeta)
	err := Client().Get(ctx, nn, cr)
	if err != nil {
		return nil, err
	}
	return cr, nil
}

func GetBackupCr(ctx context.Context, namespace string, name string) (*apiv1.MySQLBackup, error) {
	nn := types.NamespacedName{
		Namespace: namespace,
		Name:      name,
	}
	cr := new(apiv1.MySQLBackup)
	err := Client().Get(ctx, nn, cr)
	if err != nil {
		return nil, err
	}
	return cr, nil
}

func GetRestoreCr(ctx context.Context, namespace string, name string) (*apiv1.MySQLRestore, error) {
	nn := types.NamespacedName{
		Namespace: namespace,
		Name:      name,
	}
	cr := new(apiv1.MySQLRestore)
	err := Client().Get(ctx, nn, cr)
	if err != nil {
		return nil, err
	}
	return cr, nil
}

func DeleteMysqlCr(ctx context.Context, namespace string, name string) error {
	cr, err := GetMysqlCr(ctx, namespace, name)
	if err != nil {
		return err
	}
	err = Client().Delete(ctx, cr)
	return err
}

func DeleteMgrCr(ctx context.Context, namespace string, name string) error {
	cr, err := GetMgrCr(ctx, namespace, name)
	if err != nil {
		return err
	}
	err = Client().Delete(ctx, cr)
	return err
}

func DeleteBackupCr(ctx context.Context, namespace string, name string) error {
	cr, err := GetBackupCr(ctx, namespace, name)
	if err != nil {
		return err
	}
	err = Client().Delete(ctx, cr)
	return err
}

func DeleteRestoreCr(ctx context.Context, namespace string, name string) error {
	cr, err := GetRestoreCr(ctx, namespace, name)
	if err != nil {
		return err
	}
	err = Client().Delete(ctx, cr)
	return err
}

func ListPvcs(ctx context.Context, namespace, name string) (*corev1.PersistentVolumeClaimList, error) {
	pvcs := new(corev1.PersistentVolumeClaimList)
	labels := client.MatchingLabels{"v1alpha1.mysql.middleware.alauda.io/cluster": name}
	err := Client().List(ctx, pvcs, labels, client.InNamespace(namespace))
	return pvcs, err
}

func DeletePvcs(ctx context.Context, namespace, name string) error {
	labels := client.MatchingLabels{"v1alpha1.mysql.middleware.alauda.io/cluster": name}
	err := Client().DeleteAllOf(ctx, new(corev1.PersistentVolumeClaim), client.InNamespace(namespace), labels)
	if err != nil {
		return err
	}
	return nil
}
